/**
 * 作者:李杰
 * email: maillijie@gmail.com
 */
package com.javainfo.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import com.javainfo.dao.inf.ArticleCategoryDaoIf;
import com.javainfo.entity.Article;
import com.javainfo.entity.ArticleCategory;
import com.javainfo.entity.Attachments;
import com.javainfo.entity.SubArticle;
import com.javainfo.logic.ArticleSubArticleComponent;
import com.javainfo.simpledto.ArticleCategoryDto;
import com.javainfo.simpledto.ArticleDto;
import com.javainfo.simpledto.AttachmentsDto;

public class EntityConvertUtil {
	
	private ArticleCategoryDaoIf articleCategoryDao;
	
	/**
	 * 将后台获取的数据转换成换面显示用的简单DTO
	 * 
	 * @param _articleCategory
	 * @return
	 */
	public ArticleCategoryDto convertArticleCategoryToSimpleDto(ArticleCategory _articleCategory){
		ArticleCategoryDto categoryDto = new ArticleCategoryDto();
			if(_articleCategory!=null){
			int postNo = 0;
			String lastedPost = null;
			Date date = null;
			String lastedPostUserName = null;
			for (Article _article : _articleCategory.getArticles()) {
				postNo = postNo + _article.getSubArticles().size() + 1;
				if (_article.getUpdateTime() != null
						&& (date == null || _article.getUpdateTime()
								.after(date))) {
					lastedPost = _article.getTitle();
					lastedPostUserName = _article.getUser().getUserAccount();
					date = _article.getUpdateTime();
				}
				for (SubArticle subArticle : _article.getSubArticles()) {
					if (subArticle.getUpdateTime() != null
							&& (date == null || subArticle.getUpdateTime()
									.after(date))) {
						lastedPost = subArticle.getTitle();
						lastedPostUserName = subArticle.getUser().getUserAccount();
						date = subArticle.getUpdateTime();
					}
				}
			}
			categoryDto.setLastedPost(lastedPost);
			categoryDto.setPostNo(postNo);
			categoryDto.setId(_articleCategory.getId());
			categoryDto.setArticleNo(_articleCategory.getArticles().size());
			categoryDto.setCategoryName(_articleCategory.getCategoryName());
			categoryDto.setLastedPostUserName(lastedPostUserName);
			categoryDto.setBaseCategoryFlug(_articleCategory.getBaseCategoryFlug());
		}
		return categoryDto;
	}
	
	/**
	 * 将后台获取的数据转换成换面显示用的简单DTO
	 * @param article
	 * @return
	 */
	public ArticleDto convertArticleToSimpleDto(Article article){
		ArticleDto articleDto = new ArticleDto();
		if (article != null) {
			Set<Attachments> attachmentsSet = article.getAttachmentsSet();
			List<AttachmentsDto> attachmentsDtoList = new ArrayList<AttachmentsDto>();
			for(Attachments attachments : attachmentsSet){
				attachmentsDtoList.add(convertAttachmentsToSimpleDto(attachments));
			}
			articleDto.setAttachmentsList(attachmentsDtoList);
			ArticleSubArticleComponent.setSubArticles(articleDto, article);
			int subCategoryId = article.getSubCategory()==null?-1:article.getSubCategory().getId().intValue();
			int categoryId = article.getCategory()==null?-1:article.getCategory().getId().intValue();
			ArticleCategory subArticleCategory = articleCategoryDao.getById(subCategoryId);
			ArticleCategory articleCategory = articleCategoryDao.getById(categoryId);
			articleDto.setArticleCategory(convertArticleCategoryToSimpleDto(articleCategory));
			articleDto.setSubArticleCategory(convertArticleCategoryToSimpleDto(subArticleCategory));

			articleDto.setId(article.getId());
			articleDto.setClickNo(article.getClickNo());
			articleDto.setContent(article.getContent());
			articleDto.setCreateTime(article.getCreateTime());
			articleDto.setUpdateTime(article.getUpdateTime());
			articleDto.setTitle(article.getTitle());
			articleDto.setUserName(article.getUser().getUserAccount());
			articleDto.setUserId(article.getUser().getId());
			articleDto.setPostNo(article.getSubArticles() == null ? 0 : article
					.getSubArticles().size());
			Date date = article.getCreateTime();
			articleDto.setLastPostName(article.getTitle());
			articleDto.setLastPostUserName(article.getUser().getUserAccount());
			for (SubArticle subArticle : article.getSubArticles()) {
				if (subArticle.getUpdateTime() != null
						&& (date == null || subArticle.getUpdateTime().after(
								date))) {
					articleDto.setLastPostName(subArticle.getTitle());
					articleDto.setLastPostUserName(subArticle.getUser().getUserAccount());
					date = subArticle.getUpdateTime();
					articleDto.setLastPostDate(date);
				}
			}
		}
		return articleDto;
	}
	
	/**
	 * @param attachments
	 * @return
	 */
	public AttachmentsDto convertAttachmentsToSimpleDto(Attachments attachments){
		AttachmentsDto attachmentsDto = new AttachmentsDto();
		if(attachments!=null){
			attachmentsDto.setComment(attachments.getComment());
			attachmentsDto.setFileName(attachments.getFileName());
			attachmentsDto.setFileType(attachments.getFileType());
			attachmentsDto.setHasSaveToDB(attachments.getHasSaveToDB());
			attachmentsDto.setId(attachments.getId());
			attachmentsDto.setIsSystemFile(attachments.getIsSystemFile());
			attachmentsDto.setUpdateTime(attachments.getUpdateTime());
			attachmentsDto.setUploadedPath(attachments.getUploadedPath());
			attachmentsDto.setUploadTime(attachments.getUploadTime());
		}
		return attachmentsDto;
	}

	/**
	 * @param articleCategoryDao the articleCategoryDao to set
	 */
	public void setArticleCategoryDao(ArticleCategoryDaoIf articleCategoryDao) {
		this.articleCategoryDao = articleCategoryDao;
	}
	
	
}
